package com.xyhuo.leetcode.december;

/**
 * 购买两块巧克力
 * https://leetcode.cn/problems/buy-two-chocolates/
 *
 * @time 2023/12/29
 */
public class BuyChoco {

//    public static void main(String[] args) {
//        BuyChoco bc = new BuyChoco();
//        int[] price = {98, 54, 6, 34, 66, 63, 52, 39};
//        bc.buyChoco(price, 62);
//    }

    public int buyChoco(int[] prices, int money) {
        int firstCho = prices[0], secondCho = prices[1];
        for (int i = 2; i < prices.length; i++) {
            if (firstCho < secondCho) {
                if (secondCho > prices[i]) {
                    secondCho = prices[i];
                }
            } else {
                if (firstCho > prices[i]) {
                    firstCho = prices[i];
                }
            }
        }
        if (firstCho + secondCho > money) {
            return money;
        }
        return money - firstCho - secondCho;
    }
}
